import request from '@/utils/request';
import { requestWrapper, randomInt, randomPick, paginate, randomDate } from '@/utils/mock';

// 获取通知公告列表
export function getNoticeList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/notice/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const noticeTypes = ['1', '2']; // 1通知 2公告
      const statusOptions = ['0', '1', '2']; // 0草稿 1已发布 2已撤销
      
      const notices = Array.from({ length: 20 }, (_, index) => {
        const noticeType = randomPick(noticeTypes);
        const status = randomPick(statusOptions);
        const createTime = randomDate(-30);
        
        // 创建发布时间（如果状态为已发布）
        let publishTime = null;
        if (status === '1') {
          const pubDate = new Date(createTime);
          pubDate.setMinutes(pubDate.getMinutes() + randomInt(30, 120));
          publishTime = pubDate.toISOString().replace('T', ' ').substring(0, 19);
        }
        
        return {
          noticeId: index + 1,
          title: noticeType === '1' ? `系统通知${index + 1}` : `系统公告${index + 1}`,
          content: `<p>这是一条${noticeType === '1' ? '通知' : '公告'}内容，编号为${index + 1}</p>`,
          noticeType: noticeType,
          status: status,
          publishTime: publishTime,
          createBy: 'admin',
          createTime: createTime,
          updateBy: randomInt(0, 1) ? 'admin' : null,
          updateTime: randomInt(0, 1) ? randomDate(-15) : null,
          remark: randomInt(0, 1) ? `${noticeType === '1' ? '通知' : '公告'}${index + 1}的备注说明` : ''
        };
      });
      
      // 处理搜索条件
      let filteredNotices = [...notices];
      
      if (params) {
        if (params.title) {
          filteredNotices = filteredNotices.filter(item => 
            item.title.includes(params.title)
          );
        }
        
        if (params.noticeType) {
          filteredNotices = filteredNotices.filter(item => 
            item.noticeType === params.noticeType
          );
        }
        
        if (params.status) {
          filteredNotices = filteredNotices.filter(item => 
            item.status === params.status
          );
        }
        
        if (params.beginTime && params.endTime) {
          const beginTime = new Date(params.beginTime);
          const endTime = new Date(params.endTime);
          filteredNotices = filteredNotices.filter(item => {
            const createTime = new Date(item.createTime);
            return createTime >= beginTime && createTime <= endTime;
          });
        }
      }
      
      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const total = filteredNotices.length;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = Math.min(startIndex + pageSize, total);
      const list = filteredNotices.slice(startIndex, endIndex);
      
      return {
        list,
        total,
        pageNum: pageNum,
        pageSize: pageSize
      };
    }
  );
}

// 获取通知公告详情
export function getNoticeDetail(noticeId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/notice/${noticeId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      noticeId: parseInt(noticeId),
      title: `系统${parseInt(noticeId) % 2 === 0 ? '公告' : '通知'}${noticeId}`,
      content: `<p>这是通知${noticeId}的详细内容，包含了一些<strong>重要信息</strong>。</p><p>请相关人员注意查看并及时处理。</p>`,
      noticeType: parseInt(noticeId) % 2 === 0 ? '2' : '1', // 1通知 2公告
      status: '1',
      publishTime: '2023-06-01 10:00:00',
      createBy: 'admin',
      createTime: '2023-06-01 09:30:00',
      updateBy: 'admin',
      updateTime: '2023-06-01 09:45:00',
      remark: `${parseInt(noticeId) % 2 === 0 ? '公告' : '通知'}${noticeId}的备注说明`
    }
  );
}

// 新增通知公告
export function addNotice(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/notice',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      noticeId: randomInt(100, 999)
    }
  );
}

// 修改通知公告
export function updateNotice(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/notice',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除通知公告
export function deleteNotice(noticeIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/notice/${noticeIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 发布通知公告
export function publishNotice(noticeId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/notice/publish/${noticeId}`,
      method: 'put'
    }),
    // 模拟数据
    null
  );
}

// 撤销通知公告
export function cancelNotice(noticeId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/notice/cancel/${noticeId}`,
      method: 'put'
    }),
    // 模拟数据
    null
  );
} 